package com.sx.pipeline.ui.ledger.details

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActDeviceDetailsBinding
import com.sx.pipeline.ui.ledger.LedgerVM
import com.sx.pipeline.utils.AndroidUtil
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.indicator.CircleIndicator


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 设备详情
 */
@Route(path = RoutePath.Mine.DEVICE_DETAIL)
class DeviceDetailsActivity :
    CYDataBindingBaseActivity<ActDeviceDetailsBinding,LedgerVM>(R.layout.act_device_details), View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var deviceId: Int? = 0
    @JvmField
    @Autowired(name = IntentKey.ISEDIT)
    var isEdit: Boolean? = false
    @JvmField
    @Autowired(name = IntentKey.ISCALL)
    var isCall: Boolean? = false

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        if (isEdit == true){
            mBinding.tvEdit.visibility=View.GONE
        }
        if (isCall == true){
            mBinding.tvCall.visibility=View.VISIBLE
        }

    }

    override fun onResume() {
        super.onResume()
        //获取设备详情数据
        deviceId?.let { mViewModel.getDeviceDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.deviceDetailDataEvent){
            it?.productImage?.split(",")?.let { it1 -> initBanner(it1) }
            it?.let {
                //设备名称
                mBinding.tvDeviceName.text=it.name
                mBinding.sbDeviceName.rightText=it.name
                //产品型号
                mBinding.tvDeviceModel.text="产品型号："+it.productModel
                mBinding.sbDeviceModel.rightText=it.productModel
                //出厂日期
                mBinding.tvFactoryTime.text="出厂日期："+it.productionDate
                mBinding.sbFactoryTime.rightText=it.productionDate
                //产品制造厂家
                mBinding.sbProductManufacturer.rightText=it.manufacturer
                //产品编号
                mBinding.sbProductNumber.rightText=it.productNumber
                //设备所属单位
                mBinding.sbProductUnit.rightText=it.unitName
                //联系人
                mBinding.sbContact.rightText=it.contact
                //联系电话
                mBinding.sbContactNumber.rightText=it.contactPhone
            }
        }
    }

    private fun initBanner(list:List<String>){
        mBinding.banner.setAdapter(ImageAdapter(list))
        mBinding.banner.isAutoLoop(false)
        mBinding.banner.setIndicator(CircleIndicator(this))
        mBinding.banner.setIndicatorGravity(IndicatorConfig.Direction.CENTER)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_edit->{
                //编辑资料
                deviceId?.let { RouteManager.Mine.intentToDeviceAdd(2, it) }
                finish()
            }
            R.id.tv_call->{
                //拨打电话
                mViewModel.deviceDetailDataEvent.value?.contactPhone?.let {
                    AndroidUtil.toCallPhone(
                        it,this)
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        mBinding.banner.start()
    }

    override fun onStop() {
        super.onStop()
        mBinding.banner.stop()
    }
}